/****************************************************************************
 * Alacarte Wii
 * Keitaro Skywalker 2010
 * 
 * The cake is a LIE!!!!!
 * The cake is a LIE!!!!!
 * The cake is a LIE!!!!!
 *
 * alacarte.cpp
 ***************************************************************************/

#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ogcsys.h>
#include <unistd.h>
#include <wiiuse/wpad.h>
#include <fat.h>

#include "FreeTypeGX.h"
#include "video.h"
#include "audio.h"
#include "menu.h"
#include "input.h"
#include "filelist.h"
#include "alacarte.h"

#include "lualib.h"
#include "lauxlib.h"
#include "lua.h"

#include "fatmounter.h"


struct SApplicationContext appContext;

int ExitRequested = 0;

/*
 * TODO List:
 * - Learn C, C++, MakeFile...
 * - Read manuals, tutorials...
 * - Do the MWF... :D
 */

/**
 * Exit application and return to Homebrew Channnel 
 * (Homebrew Channel is a Homebrew Manager.. Just like Alacarte.. AHHHH NO! :P)
 */
void ExitApp() {
	ShutoffRumble();
	StopGX();
	
	if (appContext.exitType == EXIT_SHUTDOWN) {
		SYS_ResetSystem(SYS_POWEROFF, 0, 0);
	} else if (appContext.exitType == EXIT_REBOOT) {
		SYS_ResetSystem(SYS_RESTART, 0, 0);
	}
	
	exit(0);
}


// XML is a giant and ugly file format...   

//TODO: Implement the options screen and storage (xml format?)
// Let's see something like JSon or simplified conf files...
void InitSettings() {
	appContext.exitRequested = false;
	appContext.exitType = -1;
	
	appContext.initialMenu = MENU_DIAL_PAD;
	
	//TODO: Skin support?
	//TODO: Background music changes support?

	//Load the settings from file...
}


void PowerOff() {
	appContext.exitRequested = true;
	appContext.exitType = EXIT_SHUTDOWN;
}

void Reset() {
	appContext.exitRequested = true;
	appContext.exitType = EXIT_REBOOT;
}


unsigned int *xfb = NULL;
extern bool textVideoInit;
u8 dbvideo =0;

void InitTextVideo ()
{
    if (textVideoInit)
    {
    	//gprintf("...0\n");
        return;
    }
    dbvideo=1;
    VIDEO_Init();
	
	// get default video mode
    GXRModeObj *vmode = VIDEO_GetPreferredMode(NULL);

    // widescreen fix
    VIDEO_Configure (vmode);

    // Allocate the video buffers
    xfb = (u32 *) MEM_K0_TO_K1 (SYS_AllocateFramebuffer (vmode));

    // A console is always useful while debugging
    console_init (xfb, 20, 64, vmode->fbWidth, vmode->xfbHeight, vmode->fbWidth * 2);

    // Clear framebuffers etc.
    VIDEO_ClearFrameBuffer (vmode, xfb, COLOR_BLACK);
    VIDEO_SetNextFramebuffer (xfb);

    VIDEO_SetBlack (FALSE);
    VIDEO_Flush ();
    VIDEO_WaitVSync ();
    if (vmode->viTVMode & VI_NON_INTERLACE)
        VIDEO_WaitVSync ();

    //send console output to the gecko
    //gprintf("...1\n");

}

/**
 * Main function.
 * 
 * Called when application inits.
 * Initialize the main settings and go to GUI screen.
 * 
 * @param argc
 * @param argv
 * @return exit code
 * 
 */
int main(int argc, char *argv[]) {

	InitTextVideo();
	
	printf("Starting Alacarte...\n");
	printf("\n\tInitializing controllers");

	/** PAD_Init has to be before InitVideo don't move that **/
	PAD_Init();                                   // initialize PAD/WPAD
	
	
	printf("\n\tInitialize sd card");
	SDCard_Init();                                // mount SD for loading cfg's
	
	InitVideo(); // Initialize video
	SetupPads(); // Initialize input
	InitAudio(); // Initialize audio
	fatInitDefault(); // Initialize file system
	InitFreeType((u8*) font_ttf, font_ttf_size); // Initialize font system
	InitGUIThreads(); // Initialize GUI

	InitSettings();
	WPAD_SetDataFormat(WPAD_CHAN_ALL,WPAD_FMT_BTNS_ACC_IR);
	
	
	WPAD_SetPowerButtonCallback((WPADShutdownCallback)PowerOff);
	SYS_SetPowerCallback(PowerOff);
	SYS_SetResetCallback(Reset);
	 
	MainMenu(appContext.initialMenu);
}
